#include"process.h"

//调整网速
const int base = 100;

//声明总下载量
double total = 2048.0; // 2048MB

//但此下载
double once = 0.1;

//此时重命名函数指针
typedef void(*flush_t)(double, double);

void download(flush_t f) 
{
    //声明当前总下载量
    double current = 0.0;
    while(current < total) 
    {
        //产生 [1 - 20] 的随机数
        int r = rand() % base + 1;
        //当前网速
        double speed = r * once;
        current += speed;

        //此时可能会产生问题，current + 网速下载量 > total
        if (current >= total) {
            current = total;
        }
        
        f(total, current);
        fflush(stdout);
        usleep(5000);
    }
    printf("\n");
}

int main()
{
    srand(time(NULL));//种下随机数种子
    
    //因为参数类型为函数指针
    //此时只需要传入函数名即可
    download(FlushProcess);
    //Process();   
    
    //这部分是Linux中新增的代码
    download(FlushProcess);
    download(FlushProcess);
    download(FlushProcess);
    //Process();    

    //这部分是Windows中新增的代码
    download(FlushProcess);
    download(FlushProcess);
    download(FlushProcess);
    download(FlushProcess);

    return 0;
}
